import java.sql.*;

public class tasks {

	static Connection connection = null;



	/*public static String checkUserType(String username){
		String type, userType;
		PreparedStatement pS;
		ResultSet rS;
		connect();
		String ret;
		
		//boolean ret = false;
		try {
			
			userType = "member";
			pS = connection.prepareStatement("SELECT user.username FROM ?" +
					" WHERE user.Username = ? ");
			pS.setString(1, userType);
			pS.setString(2, username);
			rS = pS.executeQuery();
			if (rS.next()) {
				//System.out.println("Logged in");

				ret="member";
			}
			else{
				//System.out.println("Did not find user");
				ret= false;
			}
		} catch (SQLException e) {	
			e.printStackTrace();
		}
		disconnect();


		return type;
	}*/


	public static void addCreditCard(String cardNo, String CVV, String name, 
			String expire, String billAdd){
		PreparedStatement pS;
		connect();
		try {
			pS = connection.prepareStatement("insert into creditcard values (?,?,?,?,?)");
			pS.setString(1, cardNo);
			pS.setString(2, CVV);
			pS.setString(3, name);
			pS.setString(4, expire);
			pS.setString(5, billAdd);
			pS.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		disconnect();
	}


	//Searches DB for a user and pass combo and returns true if in DB and false if not.
	public static boolean login( String userName, String password){
		//loginGUI gui = new loginGUI();
		PreparedStatement pS;
		ResultSet rS;
		connect();
		boolean ret = false;
		try {
			pS = connection.prepareStatement("SELECT user.username, user.password FROM user" +
					" WHERE user.Username = ? AND  user.Password = ?");
			pS.setString(1, userName);
			pS.setString(2, password);
			rS = pS.executeQuery();
			if (rS.next()) {
				//System.out.println("Logged in");

				ret=true;
			}
			else{
				//System.out.println("Did not find user");
				ret= false;
			}
		} catch (SQLException e) {	
			e.printStackTrace();
		}
		disconnect();
		return ret;


	}

	// Creates a user and inserts into the user table.
	public static void createAccount( String userName, String password,String type){
		PreparedStatement pS;
		connect();
		try {

			//first insert into the USER table.
			pS = connection.prepareStatement("insert into user values (?,?)");
			pS.setString(1, userName);
			pS.setString(2, password);
			pS.executeUpdate();

			//Then check if a employee or regular member
			//May have to change the type check  method.

			pS = connection.prepareStatement("insert into GTCREmployee values (?)");
			pS.setString(1, userName);
			pS.executeUpdate();




		} catch (SQLException e) {
			e.printStackTrace();
		}
		disconnect();	
	}

	public static void createMemberAccount( String userName, String fName,String lName,
			String middle, String address, String phone, String email, String cardNo,
			String drivingPlan, String password){
		PreparedStatement pS;
		connect();
		try {
			//first insert into the USER table.

			pS = connection.prepareStatement("insert into user values (?,?)");
			pS.setString(1, userName);
			pS.setString(2, password);
			pS.executeUpdate();

			pS = connection.prepareStatement("insert into member values (?,?,?,?,?,?,?,?,?)");
			pS.setString(1, userName);
			pS.setString(2, fName);
			pS.setString(3, lName);
			pS.setString(4, middle);
			pS.setString(5, address);
			pS.setString(6, phone);
			pS.setString(7, email);
			pS.setString(8, cardNo);
			pS.setString(9, drivingPlan);
			pS.executeUpdate();



		} catch (SQLException e) {
			e.printStackTrace();
		}
		disconnect();	
	}


	public static void connect(){
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.
					getConnection(
							"jdbc:mysql://academic-mysql.cc.gatech.edu/cs4400_Group_25", 
							"cs4400_Group_25", "edAc9upL");

		} 
		catch(Exception e) {
			System.err.println("Exception: " + e.getMessage());
		} 
	}	
	public static void disconnect(){
		try {
			if(connection != null)
				connection.close();

		} catch(SQLException e) {}
	}


}
